<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval' ">
    <title>找师傅APP插件Demo</title>
    <link rel="stylesheet" href="style.css" media="screen" title="no title" charset="utf-8">
  </head>
  <body>
    <header class="header">
      <h1><a class="btn-back" href="./index.html">首页</a>推送演示</h1>
    </header>
    <form>
      <ul>
        <li>
          <label for="userId">用户ID:</label>
          <input type="text" name="userId" />
        </li>
        <br/>
        <li>
          <label for="role">角色:</label>
          <input type="checkbox" name="role" value="业主"/>业主
          <input type="checkbox" name="role" value="师傅"/>师傅
          <input type="checkbox" name="role" value="供应商"/>供应商
        </li>
        <br/>
        <li>
          <label for="location">所在地区:</label>
          <input type="checkbox" name="location" value="白云"/>白云
          <input type="checkbox" name="location" value="天河"/>天河
          <input type="checkbox" name="location" value="越秀"/>越秀
        </li>
        <br/>
        <li>
          <input type="submit" value="更新标签" id="submit-btn">
          <input type="button" name="name" value="getPushData" id="get-btn">
        </li>
        <div id="tagAliasResult"></div>
      </ul>
    </form>
    <script type="text/javascript" src="../cordova.js"></script>
    <script type="text/javascript">
      document.addEventListener('deviceready', function() {
        alert('ready')
        var onGetRegistradionID = function(data) {
            try{
                alert("JPushPlugin:registrationID is "+data)}
            catch(exception){
                alert(exception);
            }
        }
        window.plugins.jPushPlugin.init();
        window.plugins.jPushPlugin.getRegistrationID(onGetRegistradionID);

      })

      document.querySelector('#get-btn').addEventListener('click', function(e){
        e.preventDefault();
        window.plugins.jPushPlugin.getPushData(function(data) {
          alert(data)
        })
      })


      document.querySelector('#submit-btn').addEventListener('click', function(e) {
        e.preventDefault();
        var data = {
          userId: '',
          roles: [],
          locations: []
        };
        var $userId = document.querySelector('[name="userId"]');
        var $roles = document.querySelectorAll('[name="role"]');
        var $locations = document.querySelectorAll('[name="location"]');
        data['userId'] = $userId.value;
        for (var i = 0; i < $roles.length; i++) {
          var $role = $roles[i];
          if ($role.checked) {
            data['roles'].push($role.value);
          }
        }
        for (var i = 0; i < $locations.length; i++) {
          var $location = $locations[i];
          if ($location.checked) {
            data['locations'].push($location.value);
          }
        }
        var tags = [];
        data.roles.map(function(item) {
          tags.push(item)
        })
        data.locations.map(function(item) {
          tags.push(item)
        })
        if (data.userId !== '') {
          window.plugins.jPushPlugin.setAlias(data.userId)
          alert('setAlias: ' + data.userId)
        }
        if (tags.length > 0) {
          window.plugins.jPushPlugin.setTags([tags.toString()]);
          alert('setTags: ' + tags.toString())
        }
      })
    </script>
  </body>
</html>
